﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Procesos_Etapas : System.Web.UI.Page
{
    private bool Add
    {
        get
        {
            if (ViewState["add"] != null)
            {
                return (bool)ViewState["add"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["add"] = value;
        }
    }
    private bool Edit
    {
        get
        {
            if (ViewState["Edit"] != null)
            {
                return (bool)ViewState["Edit"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["Edit"] = value;
        }
    }
    private bool View
    {
        get
        {
            if (ViewState["View"] != null)
            {
                return (bool)ViewState["View"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["View"] = value;
        }
    }
    private bool Delete
    {
        get
        {
            if (ViewState["Delete"] != null)
            {
                return (bool)ViewState["Delete"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["Delete"] = value;
        }
    }
    protected long ProcessId
    {
        get
        {
            if (ViewState["ProcessId"] == null) return 0;
            return (long)ViewState["ProcessId"];
        }
        set { ViewState["ProcessId"] = value; }
    }
    protected String[] Message
    {
        get
        {
            if (Session["Message"] == null)
            {
                String[] m = { "", "" };
                return m;
            }
            return (String[])Session["Message"];
        }
        set { Session["Message"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetupPageByUserRoles();
            divEmpty.Visible = false;
            if (Request.QueryString["id"] != null)
            {
                long id = 0;
                long.TryParse(Request.QueryString["id"].ToString(), out id);
                ProcessId = id;
                if (id == 0)
                {
                    String[] error = { Resources.Resource.ERROR, Resources.Resource.PROCESS_NOT_FOUND };
                    Message = error;
                    Response.Redirect("~/Procesos/Default.aspx");
                }
                else
                {
                    BLLProcess bll = new BLLProcess();
                    Full_Process p = bll.getFullProcessById(id);
                    if (p != null)
                    {
                        ltrTitle.Text = p.name;
                        rptStages.DataSource = p.stages;
                        rptStages.DataBind();
                        if (p.stages.Count == 0)
                            divEmpty.Visible = true;
                        hypAdd.NavigateUrl = "~/Procesos/Etapas/Agregar.aspx?processID="+p.id;
                    }
                    else
                    {
                        String[] error = { Resources.Resource.ERROR, Resources.Resource.PROCESS_NOT_FOUND };
                        Message = error;
                        Response.Redirect("~/Procesos/Default.aspx");
                    }
                }
            }
            else
            {
                String[] error = { Resources.Resource.ERROR, Resources.Resource.PROCESS_NOT_FOUND };
                Message = error;
                Response.Redirect("~/Procesos/Default.aspx");
            }
            
        }
    }
    protected string GetConfirmValue(string value)
    {
        return "return confirmMessage('Confirmar','Est&aacute; seguro de eliminar esta etapa?','"+value+"');";
    }
    protected bool GetBoolValue(string value)
    {
        bool result = false;
        try
        {
            if (value.ToLower() == "true") result = true;
            else result = false;
        }
        catch
        {
        }
        return result;
    }
    
    
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            GridView grid = (GridView)(e.Item.FindControl("grid"));
            Process_Stages stage = (Process_Stages)e.Item.DataItem;
            if (stage != null)
            {
                grid.DataSource = stage.products;
                grid.DataBind();
            }

            if (!Delete)
            {
                ImageButton imgDelete = (ImageButton)(e.Item.FindControl("imgDelete"));
                imgDelete.Enabled = false;
                imgDelete.ToolTip = "";
            }
            
        }
    }
    protected void rptStages_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.Equals("Edit"))
        {
            long ID = 0;
            long.TryParse(e.CommandArgument.ToString(), out ID);
            this.Response.Redirect("Editar.aspx?id=" + ID.ToString() + "&processId="+this.ProcessId);
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        BLLStages bll = new BLLStages();
        int id = 0;
        int.TryParse(hdnId.Value, out id);
        if (bll.Delete(id, this.ProcessId))
        {
            String[] error = { Resources.Resource.SUCCESS, Resources.Resource.STAGEDELETED };
            Message = error;
        }
        else
        {
            String[] error = { Resources.Resource.ERROR, Resources.Resource.UPDATE_ERROR };
            Message = error;
        }
        Response.Redirect("~/Procesos/Etapas/Default.aspx?id="+this.ProcessId.ToString());
    }
    protected void SetupPageByUserRoles()
    {
        List<SessionPermissions> userPermissions = new List<SessionPermissions>();
        userPermissions = (List<SessionPermissions>)Session[GlobalNames.UserPermissions];
        userPermissions = (from upb in userPermissions
                           where upb.ModuleId == (long)GlobalEnums.Modules.Procesos
                           select upb).ToList();
        if (userPermissions.Count() > 0)
        {
            foreach (SessionPermissions up in userPermissions)
            {
                if (up.PermissionId == (long)GlobalEnums.Permissions.Agregar)
                    Add = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Editar)
                    Edit = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Ver)
                    this.View = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Eliminar)
                    this.Delete = true;
            }
            if (!View)
                this.Response.Redirect("~/Default.aspx");
            if (ProcessId > 0 && !Edit)
                hypAdd.Visible = false;
        }
        else
        {
            this.Response.Redirect("~/Default.aspx");
        }
    }
     protected void imgExport_Click(object sender, ImageClickEventArgs e)
    {
        string filename = Util.ExportToExcel(this.ProcessId, Server.MapPath("~/Exports/"));
        if (!String.IsNullOrEmpty(filename))
        {
            String p = Path.GetFileName(filename);
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + p);

            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.WriteFile(filename);

            Response.End();
        }
        else
        {
            ((Literal)Page.Master.FindControl("ltrShowMessage")).Text = "1";
            ((Literal)Page.Master.FindControl("ltrMessage")).Text = Resources.Resource.UPDATE_ERROR;
            ((Literal)Page.Master.FindControl("ltrMessageType")).Text = Resources.Resource.ERROR;
        }
    }
}